var config=require("../config.js");
var ble=require("./blue.js");
var cmds = require("./cmd.js");
var shop_id =is_null(wx.getStorageSync("shop_id"));
//排行榜
function ranking_list(func){
    wx.showNavigationBarLoading();    
    wx.request({
      url: config.sort_today,
      data:{
          shop_id:shop_id
      },
      success:function(data){          
          func(data.data);
      },
      complete:function(){
         wx.hideNavigationBarLoading();
         wx.stopPullDownRefresh();
      }
    })
}
/**
 * 历史赞列表
 */
function receive_likes(func){
   wx.showNavigationBarLoading();
   var id = wx.getStorageSync("userinfo").bracelet_id;   
   wx.request({
     url: config.receive_likes,
     data:{
          id:id    
     },
     success:function(data){
          func(data.data); 
     },
     complete:function(){
       wx.hideNavigationBarLoading();
       wx.stopPullDownRefresh();
     }
   })
}
//判断为空
function is_null(data){
  if(data=='undefined'||data==null||data==''){
      return 0;
  }else{
    return data;
  }   
}
//保存个人信息
function update_info(obj){
    wx.showLoading({
      title: '正在提交',
    });
    wx.request({
      url: config.update_info,
      data:{
        id:obj.bracelet_id,
        nick_name:obj.nick_name,
        sex:obj.sex,
        birthday:obj.birthday,
        age:obj.age,
        height:obj.height,
        weight:obj.weight
      },
      success:function(data){
        wx.hideLoading();
        if(data.data.flag){
           wx.setStorageSync("userinfo", obj);
           //set4(height=178,weight=60,sex=0,age=28)
            ble.blue_write(cmds.set4(obj.height, obj.weight, obj.sex - 1, obj.age));
            console.log("ble.blue_write");
        }
        wx.showToast({
          title: data.data.msg,
        })        
      }
    })
}
//上传数据
function upload_data(data){
   wx.request({
     url: config.upload_data,
     data: data,
     success:function(res){
         console.log("upload_data",res);
     }
   })
}
module.exports={
     ranking_list:ranking_list,
     receive_likes: receive_likes, 
     update_info: update_info,
     upload_data: upload_data
}